// pages/my/my.js
var config = require('../../comm/script/config');
var message = require('../../component/message/message')
//获取应用实例
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    userLocation: "",
    hasUserInfo: false,
    gridList: [
      { enName: 'favorite', zhName: '收藏' },
      { enName: 'history', zhName: '浏览记录' },
      { enName: 'shake', zhName: '摇一摇' },
      { enName: 'gallery', zhName: '相册' },
      { enName: 'setting', zhName: '设置' }
    ],
    skin: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(app.globalData.userInfo);
    // 检测是否存在用户信息
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        userLocation: app.globalData.userInfo.province + "," + app.globalData.userInfo.city,
        hasUserInfo: true
      });
    } else {
      // 获取用户信息
      app.getUserInfo({
        success: function (res) {
          console.log(res);
          that.setData({
            userInfo: res,
            userLocation: res.province + "," + res.city,
            hasUserInfo: true
          });
        },
        fail: function () {
          message.show.call(that, {
            content: '获取用户信息失败',
            duration: 3000
          });
        }
      }
      );
    }

  },

  getUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo === undefined) {
      message.show.call(that, {
        content: '获取用户信息失败',
        duration: 3000
      });
    } else {
      app.globalData.userInfo = e.detail.userInfo
      that.setData({
        userInfo: e.detail.userInfo,
        userLocation: e.detail.userInfo.province + "," + e.detail.userInfo.city,
        hasUserInfo: true
      });
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.getStorage({
      key: 'skin',
      success: function (res) {
        if (res.data == "") {
          that.setData({
            skin: config.skinList[0].imgUrl
          });
        } else {
          that.setData({
            skin: res.data
          });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 九宫格点击跳转
   */
  viewGridDetail: function (option) {
    var data = option.currentTarget.dataset;
    console.log(option);
    wx.navigateTo({
      url: '../' + data.url + '/' + data.url + '?barTitle=' + data.name,
    });
  },

  /**
   * 跳转到设置背景列表页面
   */
  viewSkin: function (option) {
    wx.navigateTo({
      url: '../skin/skin',
    });
  }


})